<!DOCTYPE html>
<html>
  <head>
  	<div th:include="base-libs :: base-libs" />
    <title>分配任务</title>
	
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">
    
    <style>
    	#top{
			height:47px;
			line-height:47px;
			font-size:16px;
			padding-left:35px;
			overflow:hidden;
			text-overflow: ellipsis;
			white-space:nowrap
		}
		#fixedBox{
			text-align:right;
			overflow:hidden;
			border:0;
			padding-right:30px;
			border-top:1px solid #ddd;
		}
		#fixedBox button{
			margin-right:10px;
			padding-right:20px;
		}
		#cancel{
			border: 1px solid #dedede;
		    background-color: #f1f1f1;
		    color: #333;
		}
		.laydate_body .laydate_box{
			left:77% !important;
		}
		/*浮雕字体css*/
		.fdztCss{
			text-shadow: 1px 1px 0 rgba(255, 255, 255, .7);
			color:#ccc;
		}
    </style>
	<script>
		var qyData = StoreCache.removeCache("qyData");
		var tdryData,dfwryObj;
		var saveData = []; //已分配的数据
		var lastTimePage = 1;//上一次的页数
		
		$(function(){
			//查询当前患者所属团队的团队成员
			$.QuickRemote.AjaxJson(contextRoot + "wdqyhz/queryTdry", function(result){
				tdryData = result;
				initView();
				//$("#fprwTable").datagrid({url: contextRoot + "wdqyhz/queryFwxxByQyxxids.service"});
			}, {'TDID': qyData.YLTDID});
			
			initData();
		})
		
		function initData(){
			$("#XM,#QYTDMC,#FWBMC").each(function(){
				var key = $(this).attr("id");
				$(this).html(qyData[key]).attr("title",qyData[key]);
			})
			
		}
		
		function initView(){
			//扩展datagrid编辑器类型（添加laydate时间控件）
			$.extend($.fn.datagrid.defaults.editors, {
				laydate: {
					init: function(container, options){
						var input = $('<input type="text" onclick="laydate()" class="laydate-icon" style="width:80%;border-radius:0">').appendTo(container);
						return input;
					},
					getValue: function(target){
						return $(target).val();
					},
					setValue: function(target, value){
						$(target).val(value);
					}
				}
			});
			
			$("#fprwTable").datagrid({
				idField : "FWID",
				striped : true,
				clickToSelect: true,
				fitColumns: true,
				fit:true,
				border:true,
				scrollbarSize:10,
				border: false,
				singleSelect:true,
				rownumbers: true,
				columns : [[
						{field : "FWMC",title : "服务项名称",width:60, align:'center'},
						{field : "FWNR",title : "服务项内容",width:100, align:'center',formatter:function(value){
							return '<span title="'+value+'">'+value+'</span>';
						}},
						{field : "FWBMC",title : "服务包名称",width:100, align:'center',formatter:function(value){
				    		if(StringUtils.isEmpty(value)){
				    		    return '<span class="fdztCss">医保服务项</span>';
							}else{
                                return '<span title="'+ value +'">'+ value + '</span>'
							}
						}},
						{field : "FWPC",title : "服务频次",width:50, align:'center'},
						{field : "SYFWCS",title : "剩余服务次数",width:50, align:'center',formatter:function(vavlue,row,index){
							if(row.FWPC=='无限'){
								return row.FWPC
							}else{
								return parseInt(row.FWPC || 0)-parseInt(row.YFPCS || 0);
							}	
						}},
						{field : "SFZH",title : "服务对象",width:60, align:'center',
							formatter: function(value,row){
								if(!$.isEmptyObject(row.dfwry)){
									var dfwry = row.dfwry;
									$.each(dfwry, function(i,item){
										if(item.SFZH==value){
											value = item.XM;
										}
									})
								}else{
									value = row.XM;
								}
								return value;
							},
							editor:{
	                        type: "combobox",
	                            options:{
									valueField:'SFZH',
									textField:'XM',
									editable:false
								}
	    					}
						},
						{field : "USERID",title : "服务人员",width:60, align:'center',
							formatter:function(value){
								$.each(tdryData, function(i, item){
									if(item.USERID==value){
										value = item.XM;
										return false;
									}
								})
								return value;
							},
							editor:{
	                        type: "combobox",
	                            options:{
									valueField:'USERID',
									textField:'XM',
									data: tdryData,
									editable:false
								}
	    					}
						},
						{field : "YQFWSJ",title : "预期服务时间",width:60, align:'center',
							editor:{
		                        type: "laydate"
		    					}
						
						},
				]],
				pagination: true,
				method : "post",
				url: contextRoot + "wdqyhz/queryFwxxByQyxxids.service",
				onBeforeLoad : function(params) {
					var options = $(this).datagrid("options");
					var QYXXID = qyData.QYXXID;
					if(options.url && !StringUtils.isEmpty(QYXXID)){
						params.qyxxArr = JSON.stringify(new Array(qyData));
						params.QYXXIDS = QYXXID;
						params.USERID = parent.userInfo.USERID;
						params.TDJS = 1;
						params.TDZT = 1;
					}else{
						return false;
					}
				},
				onClickRow : function(rowIndex, row){
					var editRowNum = $(this).data("editRowNum");
					if(!StringUtils.isEmpty(editRowNum) && rowIndex != editRowNum){
						$('#fprwTable').datagrid('endEdit', editRowNum);
					}
					$(this).data("editRowNum",rowIndex);
					if(row.FWPC=='无限' || parseInt(row.FWPC || 0)-parseInt(row.YFPCS || 0) > 0){
						$('#fprwTable').datagrid('beginEdit', rowIndex);
						
						//服务对象下拉框
						var ed1 = $('#fprwTable').datagrid('getEditor', {index:rowIndex,field:'SFZH'});
						if(!row.dfwry){
							//先直接把人员数据保存，避免第一次选择后数据丢失
							if($.isEmptyObject(dfwryObj)){
								row.dfwry = [{'SFZH':row.SFZH,'XM':row.XM}];
							}else{
								row.dfwry = [{'SFZH':row.SFZH,'XM':row.XM}].concat(dfwryObj[row.JMXXID]);
							}
						}
						var dfwry = new Array(row.dfwry[0]);
						if(row.FWBSX==2){
							dfwry = row.dfwry;
						}
						$(ed1.target).combobox({
							valueField: "SFZH",
						    textField: "XM",
						    data: dfwry
						});
						if(row.SFZH) $(ed1.target).combobox("setValues", row.SFZH);
					}
				},
				onLoadSuccess: function(data){
// 					alert(JSON.stringify(data))
					if(data.dfwryObj){
						dfwryObj = data.dfwryObj;
					}
					
					//载入已经分配的任务
					var rows = data.rows;
					$.each(rows, function(i,item){
							$.each(saveData, function(j,save){
								if(item.QYXXID == save.QYXXID && item.FWBID==save.FWBID && item.FWID == save.FWID){
									$('#fprwTable').datagrid('updateRow',{
										index: i,
										row: save
									});
	
	
								}
							})
					})
				},
			}); 
			
			//重载分页，为分页添加方法
            $('#fprwTable').datagrid('getPager').pagination({
                pageList: [10, 20, 40, 50, 100], //可以设置每页记录条数的列表 
                pageSize: 10,
                onSelectPage:function(page,rows){ 
		    		   if(getSaveData()){
			    		   var params = {
				    		   page: page,
				    		   rows: rows,
				    		   QYXXIDS: qyData.QYXXID,
				    		   USERID: parent.userInfo.USERID,
				    		   TDJS: 1,
				    		   TDZT: 1,
				    	   };
			    		   $.QuickRemote.AjaxJson(contextRoot + "wdqyhz/queryFwxxByQyxxids", function(result){
			    			   $('#fprwTable').datagrid("loadData",result)
			    		   }, params);
			    		   lastTimePage = page;
		    		   }else{
		    			   $(this).pagination('refresh',{	// 改变选项并刷新分页栏信息
		    					pageNumber: lastTimePage
		    				});

		    		   }
			    },
            });
		}
		
		/** 翻页时获取本页需要保存的数据   */
		function getSaveData(){
			$("#fprwTable").datagrid('endEdit', $('#fprwTable').data("editRowNum")); //结束所有编辑
			
			
			var data = $("#fprwTable").datagrid("getRows");
			if(data.length==0) return true;
			
			//验证
			var errMsg,errNum=0;
			var fpfwArr = [];
			$.each(data, function(i, item){
				var SYFWCS = parseInt(item.FWPC || 0)-parseInt(item.YFPCS || 0);
				if(SYFWCS == 0) return true;
				//如果选择了服务人员、预期服务时间、服务对象中的任何一个，其他2个必须填写完毕
				if(!StringUtils.isEmpty(item.USERID) || !StringUtils.isEmpty(item.YQFWSJ)){
					errNum = i+1;
					if(StringUtils.isEmpty(item.USERID)){ 
						errMsg = '服务人员';
						return false;
					}else if(StringUtils.isEmpty(item.YQFWSJ)){
						errMsg = '预期服务时间';
						return false;
					}else if(StringUtils.isEmpty(item.SFZH)){
						errMsg = '服务对象';
						return false;
					}
					
					//数据是否已经存在于saveData数组
					var newSaveData = true;
					$.each(saveData, function(i,data){
						if(data.QYXXID == item.QYXXID && data.FWBID==item.FWBID && data.FWID == item.FWID){
							newSaveData = false;
							saveData.splice(i, 1);//delete
							return false;
						}
					})
					saveData.push(item);
				}
			})
			
			if(StringUtils.isEmpty(errMsg)){
				return true;
			}else{
				errMsg = '您的第<span style="color:red">'+errNum+'</span>数据的<span style="color:red">' + errMsg + '</span>还未设置！';
				$.QuickAlert.alertFail({content: errMsg});
				return false;
			}
		}
		
		/** 确认分配服务  */
		function confirFprw(){
			if(getSaveData()){
				if(saveData.length==0){
					$.QuickAlert.alertFail({content: "没有需要保存的数据"});
					return;
				}
				$.QuickRemote.AjaxJson(contextRoot + "wdqyhz/save", function(result){
					if(result.state){
						$.QuickAlert.alertNormal(result.info.success, {callback: function(){
							cancel();
						}});
					}else{
						var info = result["info"];
						$.QuickAlert.alertFail({content: info["faultInfo"]});
					}
				}, {"fpfwArr":JSON.stringify(saveData), "method":"add"});
			}
		}
		
		/** 取消  */
		function cancel(){
			parent.layer.closeAll();
		}
	</script>
  </head>
  
  <body class="easyui-layout" fit="true" style="overflow:hidden;" border="false">
		<div region="north" id="top" border="false" onmouseover="javascript:this.title=$(this).text()">
			您已选中居民为<span style="color:red" id="XM"></span>，
			签约团队为<span style="color:red" id="QYTDMC"></span>，
			签约服务包为<span style="color:red" id="FWBMC"></span>
		</div>
		<div region="center" style="overflow: hidden;border-bottom:1px solid #ddd" border="false">
			<table id="fprwTable" border="false" style="border-bottom:1px solid #ddd">
			</table>
		</div>
		<div region="south" id="fixedBox" class="layui-layer-btn" style="height:50px;line-height:50px;">
			<button class="layui-layer-btn0" type="button" onclick="confirFprw()">确认</button>
			<button id="cancel" class="layui-layer-btn1" type="button" onclick="cancel()">取消</button>
		</div>
  </body>
</html>
